package yxy.game.pm2.module.fortdefeat;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

public class FortDefeatModule extends PlayerModule {
	// bean===========================================================================

	private FortDefeatType type;// 今日选择的难度类型
	private ArrayList<FortDefeatType> finished = new ArrayList<>();
	private int mapId;// 完成的关卡id

	private ArrayList<Integer> box = new ArrayList<>();// 已经领取的宝箱
	private ArrayList<Currency> hasRewards = new ArrayList<>();// 已经获得的奖励

	private int sweepId;// 可以扫荡的id

	private DateTime lastTime;

	private int[] angers;// 保存的怒气

	private Format format;//

	private HashMap<String, Integer> heroHp = new HashMap<>();// 武将损失血量万分比

	private int reviveTimes;// 复活次数

	public FortDefeatType getType() {
		return type;
	}

	public void setType(FortDefeatType type) {
		this.type = type;
	}

	public ArrayList<FortDefeatType> getFinished() {
		return finished;
	}

	public void setFinished(ArrayList<FortDefeatType> finished) {
		this.finished = finished;
	}

	public ArrayList<Integer> getBox() {
		return box;
	}

	public void setBox(ArrayList<Integer> box) {
		this.box = box;
	}

	public int getMapId() {
		return mapId;
	}

	public void setMapId(int mapId) {
		this.mapId = mapId;
	}

	public ArrayList<Currency> getHasRewards() {
		return hasRewards;
	}

	public void setHasRewards(ArrayList<Currency> hasRewards) {
		this.hasRewards = hasRewards;
	}

	public int getSweepId() {
		return sweepId;
	}

	public void setSweepId(int sweepId) {
		this.sweepId = sweepId;
	}

	public DateTime getLastTime() {
		return lastTime;
	}

	public void setLastTime(DateTime lastTime) {
		this.lastTime = lastTime;
	}

	public int[] getAngers() {
		return angers;
	}

	public void setAngers(int[] angers) {
		this.angers = angers;
	}

	public Format getFormat() {
		return format;
	}

	public void setFormat(Format format) {
		this.format = format;
	}

	public HashMap<String, Integer> getHeroHp() {
		return heroHp;
	}

	public void setHeroHp(HashMap<String, Integer> heroHp) {
		this.heroHp = heroHp;
	}

	public int getReviveTimes() {
		return reviveTimes;
	}

	public void setReviveTimes(int reviveTimes) {
		this.reviveTimes = reviveTimes;
	}
	// func===========================================================================

	/** 刷新每日的状态 */
	private void update() {
		try {
			if (tryLock()) {
				DateTime now = DateTime.now();
				if (getLastTime() == null) {
					setLastTime(now);
					this.set();
					return;
				}
				if (!new LocalDate(now).equals(new LocalDate(getLastTime()))) {
					int between = Days.daysBetween(new LocalDate(getLastTime()), new LocalDate(now)).getDays();
					setLastTime(now);
					if (between == 1) {
						sweepId = mapId - 10;// XXX 过关斩将扫荡10次目前是代码写死的
						yxy.cherry.data.bean.running _running = DataCenter.getData(sweepId, yxy.cherry.data.bean.running.class);
						if (_running == null) {
							sweepId = 0;
						}
					} else {
						setSweepId(0);
					}

					setType(null);
					hasRewards.clear();
					box.clear();
					mapId = 0;
					setAngers(null);
					heroHp.clear();
					setReviveTimes(0);

					this.set();
				}
				check();
			}
		} catch (Exception e) {
		} finally {
			unlock();
		}
	}

	public void select(FortDefeatType type) {
		if (getType() != null || type == null) {
			return;
		}
		setType(type);
		if (sweepId != 0) {
			// 检查扫荡的数据
			yxy.cherry.data.bean.running _running = DataCenter.getData(sweepId, yxy.cherry.data.bean.running.class);
			if (_running == null || _running.getDifficulty() != type.getNumber()) {
				sweepId = 0;
			}
		}
		set();
		check();
	}

	public void finish() {
		for (FortDefeatType type : getFinished()) {
			if (type.equals(getType())) {
				return;
			}
		}
		getFinished().add(getType());
	}

	public void check() {
		new TipHandle().updateTip(getPlayerCode(), TipType.历练_过关斩将切页, getType() == null);
	}

	/** 合并已获得奖励 */
	public void hasrewards(Collection<Currency> rewards) {
		hasRewards.addAll(rewards);
		hasRewards = new ArrayList<>(Currency.merge(hasRewards));
	}

	// redis==========================================================================
	private static String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:FORTDEFEAT", playerCode);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public FortDefeatModule get(String playerCode) {
		try {
			FortDefeatModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), FortDefeatModule.class);
				if(module != null){module.set();}//把数据同步到redis
			}

			if (module == null) {
				module = create(playerCode);
			}
			module.update();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	static private FortDefeatModule create(String playerCode) {
		FortDefeatModule module = new FortDefeatModule();
		module.setPlayerCode(playerCode);
		return module;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				FortDefeatModule fortDefeatModule = getv(key(playerCode));
				MySQLCacheDAO.setV(key, fortDefeatModule);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
